<?php
/**
 * 房屋管理
 * User: hls
 * Date: 2016/3/21
 * Time: 10:10
 */

namespace Admin\Controller;
use Admin\BaseController;
use Common\Model\BusinessModel;
use Common\Model\RoomModel;
use Common\Model\UserModel;
use Common\Model\UtilsModel;
use Think\Controller;
use Think\Db;
use Think\Page;

class RoomController extends BaseController
{

    /**
     * 门店列表
     */
    public function roomList(){

        $where  = '1';

        $userM  = M('r_room',null,DB_MAIN_CFG);
        $list =$userM
            ->where($where)
            ->order('rid desc')
            ->select();



        $this->smarty->assign('list', $list);

        //$auditStatusKV=UserModel::getAuditStatus();

        //$this->smarty->assign('auditType', $auditStatusKV);
      //  $this->smarty->assign('selectedAuditType', $auditStatus);
        $this->smarty->assign('bedKv',RoomModel::getBedKv());
        $this->smarty->assign('isOnline',RoomModel::ROOM_ONLINE);

        $this->smarty->display('Room/room_list.html');
    }

    /**
     * 房屋增加
     */
    public function roomAdd(){

        if(IS_POST){
            $roomName=I('post.roomName');
            $restaurantItem = M('r_room',null,DB_MAIN_CFG)->where("roomName like '{$roomName}'")->find();
            if($restaurantItem){
                $this->error('同名房间已经存在');
            }

            $album=I('post.album');
            if(count($album)<1){
                $this->error('图集至少需要一张图片');
            }

            $userM = M('r_room',null,DB_MAIN_CFG);
            $rules = array(
                array('roomName', 'require', '门店名称必须填写'),
                array('listPic', 'require','列表图必须上传'),
            );

            if($data = $userM->validate($rules)->create()){

                $data['album']=implode(',',$album);
                 $userMId=$userM->add($data);
                 $this->success('添加成功');
            }else{
                $this->error($userM->getError());
            }
        }
        else{

            $this->smarty->assign('bedKv',RoomModel::getBedKv());
            $this->smarty->display('Room/room_edit.html');
        }
    }

    /**
     * 房屋编辑
     */
    public function roomEdit(){
        if(IS_POST){
            $rid=I('post.rid',0,'intval');
            $roomName=I('post.roomName');
            $restaurantItem = M('r_room',null,DB_MAIN_CFG)->where("roomName like '{$roomName}' and rid<>{$rid}")->find();
            if($restaurantItem){
                $this->error('同名房间已经存在');
            }

            $album=I('post.album');
            if(count($album)<1){
                $this->error('图集至少需要一张图片');
            }

            $userM = M('r_room',null,DB_MAIN_CFG);
            $rules = array(
                array('roomName', 'require', '房间名称必须填写'),
                array('listPic', 'require','列表图必须上传'),
            );

            if($data = $userM->validate($rules)->create()){

                $data['album']=implode(',',$album);
                $userMId=$userM->where("rid={$rid}")->save($data);
                $this->success('修改成功');
            }else{
                $this->error($userM->getError());
            }
        }
        else{
            $rid=I('get.rid',0,'intval');
            $uinfo=M('r_room',null,DB_MAIN_CFG)->find($rid);

            $this->smarty->assign('obj',$uinfo);

            $this->smarty->assign('bedKv',RoomModel::getBedKv());
            $this->smarty->display('Room/room_edit.html');
        }
    }

    /**
     *
     */
    public function roomUpdate(){
        $rid = I('get.rid', 0, 'intval');

        $item=RoomModel::getRoomInstance()->where('rid='.$rid)->find();
        if(!$item){
            $this->error('没有指定房间');
        }
        if($item['status'] != RoomModel::ROOM_ONLINE) {
            $result = RoomModel::getRoomInstance()->where('rid='.$rid)->setField('status', RoomModel::ROOM_ONLINE);
        }else{
            $result = RoomModel::getRoomInstance()->where('rid='.$rid)->setField('status', RoomModel::ROOM_OFFLINE);
        }
        if ($result) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }


}
